package com.cash.lemoloan.statisticlib.Utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import com.cash.lemoloan.statisticlib.StatisticApplication;
import com.cash.lemoloan.statisticlib.StatisticContant;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;

public class AppInfoUtils {

    private static String sVersionName;
    private static String sAppChannel;
    private static String sAppADChannel;

    /**
     * 获取app版本号
     *
     * @return
     */
    public static int getVersionCode() {
        int versionCode = 0;
        try {
            PackageManager pm = StatisticApplication.getInstance().getPackageManager();
            PackageInfo pi = pm.getPackageInfo(StatisticApplication.getInstance().getPackageName(), 0);
            versionCode = pi.versionCode;// 获取在AndroidManifest.xml中配置的版本号
        } catch (Throwable e) {
        }
        return versionCode;
    }



    /**
     * 获取app版本号
     *
     * @return
     */
    public static String getVersionName(Context context) {
        if (TextUtils.isEmpty(sVersionName)) {
            try {
                PackageManager pm = context.getPackageManager();
                PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
                sVersionName = pi.versionName;// 获取在AndroidManifest.xml中配置的版本号
            } catch (Throwable e) {
                sVersionName = "";
            }
        }
        return sVersionName;
    }


    /**
     * 设置渠道号, 目前印尼项目未使用PackerNG等工具打渠道号，后续切换为PackerNG等工具打渠道号后就不再需要再设置
     *
     * @param channel
     */
    public static void initAppADChannel(String channel){
        if (TextUtils.isEmpty(channel))
            return;
        sAppADChannel = channel;
        PreferencesUtil.putString("adchannel",channel);
    }
    /**
     * 获取App默认渠道号
     *
     * @return 渠道号
     */
    public static String getAppDefaultChannel() {

        if(!TextUtils.isEmpty(sAppChannel)){
            return sAppChannel;
        }else{
            if(!TextUtils.isEmpty(StatisticContant.DEFAULT_CHANNEL)){
                sAppChannel = StatisticContant.DEFAULT_CHANNEL;
            }else{
                sAppChannel = "unknown";
            }
        }
        return sAppChannel;
    }
    /**
     * 获取App渠道号
     *
     * @return 渠道号
     */
    public static String getAppChannel() {
        if(!TextUtils.isEmpty(sAppADChannel)){
            return sAppADChannel;
        }
        sAppADChannel = PreferencesUtil.getString("adchannel");
        if(TextUtils.isEmpty(sAppADChannel)){
            sAppADChannel = getAppDefaultChannel();
        }
        return sAppADChannel;
    }
    /**
     * 获取应用包名
     *
     * @return
     */
    public static String getPackageName() {
        return StatisticApplication.getInstance().getPackageName();
    }
    public static void getAppDetailSettingIntent(Context context){
        Intent localIntent = new Intent();
        localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= 9) {
            localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
            localIntent.setData(Uri.fromParts("package", getPackageName(), null));
        } else if (Build.VERSION.SDK_INT <= 8) {
            localIntent.setAction(Intent.ACTION_VIEW);
            localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
            localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
        }
        context.startActivity(localIntent);
    }

    /**
     * 启动到应用商店app详情界面
     *
     * @param appPkg    目标App的包名
     * @param marketPkg 应用商店包名 ,如果为""则由系统弹出应用商店列表供用户选择,否则调转到目标市场的应用详情界面，某些应用商店可能会失败
     */
    public static void launchAppPlayUpdate(Context context,String appPkg, String marketPkg) {
        try {
            if (TextUtils.isEmpty(appPkg)) return;
            if (GoogleApiAvailability.getInstance().isGooglePlayServicesAvailable(StatisticApplication.getInstance()) != ConnectionResult.SUCCESS) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=" + appPkg));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            } else {
                Uri uri = Uri.parse("market://details?id=" + appPkg);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                if (!TextUtils.isEmpty(marketPkg)) {
                    intent.setPackage(marketPkg);
                }
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        } catch (Exception e) {
        }
    }
    /**
     * 拉起指定应用
     * @param pkg
     * @return
     */
    public static boolean launchThirdApp(Context context,String pkg){
        try {
            if(!TextUtils.isEmpty(pkg)) {
                Intent intent = context.getPackageManager().getLaunchIntentForPackage(pkg);
                if (intent != null) {
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    context.startActivity(intent);
                    return true;
                } else {
                    return false;
                }
            } else {
                return false;
            }
        } catch (Throwable e) {
            e.printStackTrace();
            return false;
        }
    }

    public static boolean checkAppInstalled(Context context,String pkgName) {
        if (pkgName== null || pkgName.isEmpty()) {
            return false;
        }
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if(packageInfo == null) {
            return false;
        } else {
            return true;//true为安装了，false为未安装
        }
    }




}
